<?php $this->_extends('_layouts/admin/main_layout'); ?>

<?php $this->_block('resource'); ?>

<?php $this->_endblock(); ?>

<?php $this->_block('title'); ?>
	Html Templates
<?php $this->_endblock(); ?>

<?php $this->_block('contents'); ?>

<div id="content" class="box">
	<h1>已添加的Html模板</h1>
	
	<div id="menu" class="box">
		<ul class="box">
			<li><a href="<?php echo url('adminhtmltemplates/searchnew');?>"><span>查找新的Html模板</span></a></li>
		</ul>
	</div>
	
	<fieldset>
		<legend>已添加的Html模板</legend>
		<table class="full-size" style="text-align: center;">
			<thead>
				<tr>
					<th style="width:3%">区域分类</th>
					<th style="width:3%">颜色分类</th>
					<th style="width:3%">行列分类</th>
					<th style="width:3%">侧边栏分类</th>
					<th style="width:3%">用途分类</th>
					
					<th style="width:5%">名称</th>
					<th style="width:10%">描述</th>
					<th style="width:5%">根目录文件夹</th>
					<th style="width:10%">缩略图 - 小</th>
					<th style="width:10%">缩略图 - 大</th>
					<th style="width:5%">预览首页</th>
					<th style="width:5%">下载包</th>
					<th style="width:3%">点击数</th>
					<th style="width:3%">下载数</th>
					<th style="width:10%">生成日期</th>
					<th style="width:5%">操作</th>
				</tr>
			</thead>
			<tbody>
				<?php if(!empty($htmlTemplatesAdded)): foreach($htmlTemplatesAdded as $ht): ?>
					<tr>
						<td class="type_area"><?php echo $ht['type_area']; ?></td>
						<td class="type_color"><?php echo $ht['type_color']; ?></td>
						<td class="type_columnwidth"><?php echo $ht['type_columnwidth']; ?></td>
						<td class="type_sidebarcorner"><?php echo $ht['type_sidebarcorner']; ?></td>
						<td class="type_using"><?php echo $ht['type_using']; ?></td>
						
						<td class="name"><?php echo $ht['name']; ?></td>
						<td class="description"><?php echo $ht['description']; ?></td>
						<td class="root_foldername"><?php echo $ht['root_foldername']; ?></td>
						<td class="thumb_small">
							<img alt="<?php echo $ht['name']; ?>" width="100" height="70" src="<?php echo $htmltemp_url . $ht['root_foldername'] . '/' . $ht['thumb_small']; ?>" />
							<?php echo $ht['thumb_small']; ?>
						</td>
						<td class="thumb_big">
							<img alt="<?php echo $ht['name']; ?>" width="100" height="70" src="<?php echo $htmltemp_url . $ht['root_foldername'] . '/' . $ht['thumb_big']; ?>" />
							<?php echo $ht['thumb_big']; ?>
						</td>
						<td class="preview_indexpage"><?php echo $ht['preview_indexpage']; ?></td>
						<td class="download_pack"><?php echo $ht['download_pack']; ?></td>
						<td><?php echo $ht['view_num']; ?></td>
						<td><?php echo $ht['download_num']; ?></td>
						<td><?php echo $ht['created_date']; ?></td>
						<td>
							<a href="javascript: void(0);" class="edit_btn" id="<?php echo $ht['id']; ?>">编辑</a>
							<a href="<?php echo url('adminhtmltemplates/index', array('ope' => 'delete', 'id' => $ht['id'])); ?>">删除</a>
						</td>
					</tr>
				<?php endforeach; endif; ?>
			</tbody>
		</table>
	</fieldset>
</div>




<form id="edit_block">
	<ul class="formErrors" id="editErrors">
	
	</ul>

	<input type="hidden" id="edit_id" name="form[id]" />

	<table>
		<tr>
			<th id="edit_foldername">asdasd</th>
		</tr>
		<tr>
			<td>模板名称:</td>
			<td>
				<input id="edit_name" name="form[name]" />
			</td>
		</tr>
		<tr>
			<td>描述:</td>
			<td>
				<textarea id="edit_desc" name="form[description]"></textarea>
			</td>
		</tr>
		<tr>
			<td>根目录文件夹:</td>
			<td>
				<input id="edit_rootfolder" name="form[root_foldername]" />
			</td>
		</tr>
		<tr>
			<td>预览图（小）:</td>
			<td>
				<select id="edit_thum_s" name="form[thumb_small]"></select>
			</td>
		</tr>
		<tr>
			<td>预览图（大）:</td>
			<td>
				<select id="edit_thum_b" name="form[thumb_big]"></select>
			</td>
		</tr>
		<tr>
			<td>预览主页:</td>
			<td>
				<select id="edit_previewindex" name="form[preview_indexpage]"></select>
			</td>
		</tr>
		<tr>
			<td>下载包:</td>
			<td>
				<select id="edit_downloadpack" name="form[download_pack]"></select>
			</td>
		</tr>
		<tr>
			<td>区域分类:</td>
			<td>
				<select id="edit_type_area" name="form[type_area_id]">
					<?php if(!empty($htmltemplateTypes['type_area'])): foreach($htmltemplateTypes['type_area'] as $type): ?>
						<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
					<?php endforeach; endif; ?>
				</select>
			</td>
		</tr>
		<tr>
			<td>颜色分类:</td>
			<td>
				<select id="edit_type_color" name="form[type_color_id]">
					<?php if(!empty($htmltemplateTypes['type_color'])): foreach($htmltemplateTypes['type_color'] as $type): ?>
						<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
					<?php endforeach; endif; ?>
				</select>
			</td>
		</tr>
		<tr>
			<td>行列分类:</td>
			<td>
				<select id="edit_type_columnwidth" name="form[type_columnwidth_id]">
					<?php if(!empty($htmltemplateTypes['type_columnwidth'])): foreach($htmltemplateTypes['type_columnwidth'] as $type): ?>
						<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
					<?php endforeach; endif; ?>
				</select>
			</td>
		</tr>
		<tr>
			<td>侧边栏分类:</td>
			<td>
				<select id="edit_type_sidebarcorner" name="form[type_sidebarcorner_id]">
					<?php if(!empty($htmltemplateTypes['type_sidebarcorner'])): foreach($htmltemplateTypes['type_sidebarcorner'] as $type): ?>
						<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
					<?php endforeach; endif; ?>
				</select>
			</td>
		</tr>
		<tr>
			<td>用途分类:</td>
			<td>
				<select id="edit_type_using" name="form[type_using_id]">
					<?php if(!empty($htmltemplateTypes['type_using'])): foreach($htmltemplateTypes['type_using'] as $type): ?>
						<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
					<?php endforeach; endif; ?>
				</select>
			</td>
		</tr>
	</table>
</form>

<script>
	$('#edit_block').dialog({
		autoOpen: false,
		height: 'auto',
		width: 'auto',
		modal: true,
		buttons: {
			保存: function(){
				$.ajax({
					url: '<?php echo url('adminhtmltemplates/edit'); ?>',
					type: 'post',
					data: $('#edit_block').serialize(),
					success: function(ret){
						if(ret.result){
							$('.currEdit').children('.type_color').html($('#edit_block #edit_type_color').find("option:selected").text());
							$('.currEdit').children('.type_area').html($('#edit_block #edit_type_area').find("option:selected").text());
							$('.currEdit').children('.type_columnwidth').html($('#edit_block #edit_type_columnwidth').find("option:selected").text());
							$('.currEdit').children('.type_sidebarcorner').html($('#edit_block #edit_type_sidebarcorner').find("option:selected").text());
							$('.currEdit').children('.type_using').html($('#edit_block #edit_type_using').find("option:selected").text());
							
							$('.currEdit').children('.name').html($('#edit_block #edit_name').val());
							$('.currEdit').children('.description').html($('#edit_block #edit_desc').val());
							$('.currEdit').children('.root_foldername').html($('#edit_block #edit_rootfolder').val());
							$('.currEdit').children('.preview_indexpage').html($('#edit_block #edit_previewindex').val());
							$('.currEdit').children('.download_pack').html($('#edit_block #edit_downloadpack').val());

							$('.currEdit').children('.thumb_small').html('<img alt="'+$('#edit_block #edit_name').val()+'" width="100" height="70" src="<?php echo $htmltemp_url; ?>'+$("#edit_block #edit_rootfolder").val()+'/'+$("#edit_block #edit_thum_s").val()+'" />'+$("#edit_block #edit_thum_s").val()+'');
							$('.currEdit').children('.thumb_big').html('<img alt="'+$('#edit_block #edit_name').val()+'" width="100" height="70" src="<?php echo $htmltemp_url; ?>'+$("#edit_block #edit_rootfolder").val()+'/'+$("#edit_block #edit_thum_b").val()+'" />'+$("#edit_block #edit_thum_b").val()+'');
							
							$('#edit_block').dialog( "close" );
						}else{
							$('#editErrors').html('');
							$.each(ret.errorinfo, function(item, errors){
								var erritems = $('<li />');
								erritems.append(item);
								var ul = $('<ul />');
								$.each(errors, function(k, error){
									ul.append('<li class="error">'+error+'</li>');
								});
								erritems.append(ul);
								$('#editErrors').append(erritems);
							});
						}
					},
					dataType: 'json'
				});
			},
			
			取消: function(){
				$('.currEdit').removeClass('currEdit');
				$(this).dialog( "close" );
			}
		},
		close: function(){
			$('.currEdit').removeClass('currEdit');
		}
	});


	$('.edit_btn').click(function(){
		$(this).parent().parent('tr').addClass('currEdit');
		
		var id = $(this).attr('id');
		$.ajax({
			url: '<?php echo url('adminhtmltemplates/getdiskinfo'); ?>',
			type: 'get',
			data: {id: id},
			success: function(ret){
				var infoDisk = ret.infoDisk;
				var infoSaved = ret.infoSaved;
				
				var files = infoDisk.file;

				$('#edit_block #edit_foldername').text(infoSaved.name);
				$('#edit_block #edit_name').val(infoSaved.name);
				$('#edit_block #edit_desc').val(infoSaved.description);
				$('#edit_block #edit_rootfolder').val(infoSaved.root_foldername);
				$('#edit_block #edit_thum_s').html('');
				$('#edit_block #edit_thum_b').html('');
				$('#edit_block #edit_previewindex').html('');
				$('#edit_block #edit_downloadpack').html('');

				var imgExts = new Array('png', 'jpg', 'jpeg', 'bmp', 'gif');
				var indexPages = new Array('index', 'main');
				var pageExts = new Array('php', 'htm', 'html');
				var zipExts = new Array('rar', 'zip');
				$.each(files, function(k, value){
					var ext = getFileExt(value);
					if(imgExts.hasVal(ext)){
						var imgObj = new Image();
						imgObj.src = '<?php echo $htmltemp_url; ?>'+ infoSaved.root_foldername + '/' + value;
						
						$('#edit_block #edit_thum_s').append('<option value="'+value+'">('+imgObj.width+'*'+imgObj.height+')'+value+'</option>');
						$('#edit_block #edit_thum_b').append('<option value="'+value+'">('+imgObj.width+'*'+imgObj.height+')'+value+'</option>');
					}

					if(pageExts.hasVal(ext)){
						$('#edit_block #edit_previewindex').append('<option value="'+value+'">'+value+'</option>');
						if(indexPages.hasVal(getFileNameNoExt(value))){
							$('#edit_block #edit_previewindex').val(value);
						}
					}

					if(zipExts.hasVal(ext)){
						$('#edit_block #edit_downloadpack').append('<option value="'+value+'">'+value+'</option>');
					}
				});

				$('#edit_block #edit_id').val(id);
				$('#edit_block #edit_thum_s').val(infoSaved.thumb_small);
				$('#edit_block #edit_thum_b').val(infoSaved.thumb_big);
				$('#edit_block #edit_previewindex').val(infoSaved.preview_indexpage);
				$('#edit_block #edit_downloadpack').val(infoSaved.download_pack);

				$('#edit_block #edit_type_area').val(infoSaved.type_area_id);
				$('#edit_block #edit_type_color').val(infoSaved.type_color_id);
				$('#edit_block #edit_type_columnwidth').val(infoSaved.type_columnwidth_id);
				$('#edit_block #edit_type_sidebarcorner').val(infoSaved.type_sidebarcorner_id);
				$('#edit_block #edit_type_using').val(infoSaved.type_using_id);
				
				$('#editErrors').html('');
				
				$('#edit_block').dialog('open');
			},
			dataType: 'json'
		});
		
	});
</script>
<?php $this->_endblock(); ?>